package com.ecommerce.repository;

import com.ecommerce.entity.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CategoryRepository extends JpaRepository<Category, Long> {
    
    // 获取所有激活的分类，按排序顺序排列
    @Query("SELECT c FROM Category c WHERE c.isActive = true ORDER BY c.sortOrder ASC, c.createdAt DESC")
    List<Category> findActiveCategoriesOrderBySortOrder();
    
    // 根据排序获取前N个分类
    @Query("SELECT c FROM Category c WHERE c.isActive = true ORDER BY c.sortOrder ASC, c.createdAt DESC")
    List<Category> findTopCategories();
    
    // 根据名称查找分类
    Category findByName(String name);
    
    // 检查分类名称是否存在
    boolean existsByName(String name);
} 